********************************************************************************
**  TITLE:      ca2015_ipsos_contact_networks                                 **  
**  AUTHOR:	    Philippe Mongrain                                             **
**  DATA:       2015 Canada General Election Exit Poll                        **
**	DATE:		October 2022 					                              **	
**  VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "ca2015_ipsos_contact_networks", replace

* Open the dataset

import spss using "2015 Canada General Election Exit Poll.sav", clear case(lower)


***********************
** DISTRICT OUTCOMES **
***********************

decode seat, generate(riding_str)
gen str riding_code = substr(riding_str,1,5)
replace riding_code = "" if riding_str == "Not specified"
destring riding_code, gen(riding)

* Election outcome in each district (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 6 = other)

gen district_outcome = .

replace district_outcome = 2 if riding == 10001
replace district_outcome = 2 if riding == 10002
replace district_outcome = 2 if riding == 10003
replace district_outcome = 2 if riding == 10004
replace district_outcome = 2 if riding == 10005
replace district_outcome = 2 if riding == 10006
replace district_outcome = 2 if riding == 10007
replace district_outcome = 2 if riding == 11001
replace district_outcome = 2 if riding == 11002
replace district_outcome = 2 if riding == 11003
replace district_outcome = 2 if riding == 11004
replace district_outcome = 2 if riding == 12001
replace district_outcome = 2 if riding == 12002
replace district_outcome = 2 if riding == 12003
replace district_outcome = 2 if riding == 12004
replace district_outcome = 2 if riding == 12005
replace district_outcome = 2 if riding == 12006
replace district_outcome = 2 if riding == 12007
replace district_outcome = 2 if riding == 12008
replace district_outcome = 2 if riding == 12009
replace district_outcome = 2 if riding == 12010
replace district_outcome = 2 if riding == 12011
replace district_outcome = 2 if riding == 13001
replace district_outcome = 2 if riding == 13002
replace district_outcome = 2 if riding == 13003
replace district_outcome = 2 if riding == 13004
replace district_outcome = 2 if riding == 13005
replace district_outcome = 2 if riding == 13006
replace district_outcome = 2 if riding == 13007
replace district_outcome = 2 if riding == 13008
replace district_outcome = 2 if riding == 13009
replace district_outcome = 2 if riding == 13010
replace district_outcome = 3 if riding == 24001
replace district_outcome = 3 if riding == 24002
replace district_outcome = 2 if riding == 24003
replace district_outcome = 2 if riding == 24004
replace district_outcome = 2 if riding == 24005
replace district_outcome = 2 if riding == 24006
replace district_outcome = 1 if riding == 24007
replace district_outcome = 1 if riding == 24008
replace district_outcome = 4 if riding == 24009
replace district_outcome = 1 if riding == 24010
replace district_outcome = 3 if riding == 24011
replace district_outcome = 3 if riding == 24012
replace district_outcome = 2 if riding == 24013
replace district_outcome = 4 if riding == 24014
replace district_outcome = 2 if riding == 24015
replace district_outcome = 2 if riding == 24016
replace district_outcome = 2 if riding == 24017
replace district_outcome = 3 if riding == 24018
replace district_outcome = 1 if riding == 24019
replace district_outcome = 1 if riding == 24020
replace district_outcome = 2 if riding == 24021
replace district_outcome = 2 if riding == 24022
replace district_outcome = 2 if riding == 24023
replace district_outcome = 2 if riding == 24024
replace district_outcome = 3 if riding == 24025
replace district_outcome = 2 if riding == 24026
replace district_outcome = 2 if riding == 24027
replace district_outcome = 3 if riding == 24028
replace district_outcome = 2 if riding == 24029
replace district_outcome = 2 if riding == 24030
replace district_outcome = 4 if riding == 24031
replace district_outcome = 3 if riding == 24032
replace district_outcome = 4 if riding == 24033
replace district_outcome = 2 if riding == 24034
replace district_outcome = 1 if riding == 24035
replace district_outcome = 2 if riding == 24036
replace district_outcome = 2 if riding == 24037
replace district_outcome = 2 if riding == 24038
replace district_outcome = 3 if riding == 24039
replace district_outcome = 2 if riding == 24040
replace district_outcome = 2 if riding == 24041
replace district_outcome = 1 if riding == 24042
replace district_outcome = 3 if riding == 24043
replace district_outcome = 2 if riding == 24044
replace district_outcome = 1 if riding == 24045
replace district_outcome = 4 if riding == 24046
replace district_outcome = 1 if riding == 24047
replace district_outcome = 4 if riding == 24048
replace district_outcome = 2 if riding == 24049
replace district_outcome = 4 if riding == 24050
replace district_outcome = 1 if riding == 24051
replace district_outcome = 2 if riding == 24052
replace district_outcome = 2 if riding == 24053
replace district_outcome = 3 if riding == 24054
replace district_outcome = 2 if riding == 24055
replace district_outcome = 2 if riding == 24056
replace district_outcome = 2 if riding == 24057
replace district_outcome = 1 if riding == 24058
replace district_outcome = 2 if riding == 24059
replace district_outcome = 4 if riding == 24060
replace district_outcome = 1 if riding == 24061
replace district_outcome = 2 if riding == 24062
replace district_outcome = 4 if riding == 24063
replace district_outcome = 3 if riding == 24064
replace district_outcome = 2 if riding == 24065
replace district_outcome = 3 if riding == 24066
replace district_outcome = 2 if riding == 24067
replace district_outcome = 2 if riding == 24068
replace district_outcome = 2 if riding == 24069
replace district_outcome = 2 if riding == 24070
replace district_outcome = 3 if riding == 24071
replace district_outcome = 2 if riding == 24072
replace district_outcome = 3 if riding == 24073
replace district_outcome = 2 if riding == 24074
replace district_outcome = 4 if riding == 24075
replace district_outcome = 3 if riding == 24076
replace district_outcome = 2 if riding == 24077
replace district_outcome = 2 if riding == 24078
replace district_outcome = 2 if riding == 35001
replace district_outcome = 3 if riding == 35002
replace district_outcome = 2 if riding == 35003
replace district_outcome = 1 if riding == 35004
replace district_outcome = 1 if riding == 35005
replace district_outcome = 2 if riding == 35006
replace district_outcome = 2 if riding == 35007
replace district_outcome = 2 if riding == 35008
replace district_outcome = 2 if riding == 35009
replace district_outcome = 2 if riding == 35010
replace district_outcome = 2 if riding == 35011
replace district_outcome = 2 if riding == 35012
replace district_outcome = 1 if riding == 35013
replace district_outcome = 1 if riding == 35014
replace district_outcome = 2 if riding == 35015
replace district_outcome = 2 if riding == 35016
replace district_outcome = 1 if riding == 35017
replace district_outcome = 2 if riding == 35018
replace district_outcome = 2 if riding == 35019
replace district_outcome = 2 if riding == 35020
replace district_outcome = 2 if riding == 35021
replace district_outcome = 1 if riding == 35022
replace district_outcome = 1 if riding == 35023
replace district_outcome = 2 if riding == 35024
replace district_outcome = 1 if riding == 35025
replace district_outcome = 3 if riding == 35026
replace district_outcome = 2 if riding == 35027
replace district_outcome = 2 if riding == 35028
replace district_outcome = 2 if riding == 35029
replace district_outcome = 1 if riding == 35030
replace district_outcome = 2 if riding == 35031
replace district_outcome = 2 if riding == 35032
replace district_outcome = 1 if riding == 35033
replace district_outcome = 1 if riding == 35034
replace district_outcome = 3 if riding == 35035
replace district_outcome = 2 if riding == 35036
replace district_outcome = 3 if riding == 35037
replace district_outcome = 2 if riding == 35038
replace district_outcome = 2 if riding == 35039
replace district_outcome = 1 if riding == 35040
replace district_outcome = 2 if riding == 35041
replace district_outcome = 2 if riding == 35042
replace district_outcome = 2 if riding == 35043
replace district_outcome = 2 if riding == 35044
replace district_outcome = 2 if riding == 35045
replace district_outcome = 1 if riding == 35046
replace district_outcome = 2 if riding == 35047
replace district_outcome = 1 if riding == 35048
replace district_outcome = 1 if riding == 35049
replace district_outcome = 1 if riding == 35050
replace district_outcome = 3 if riding == 35051
replace district_outcome = 2 if riding == 35052
replace district_outcome = 2 if riding == 35053
replace district_outcome = 2 if riding == 35054
replace district_outcome = 2 if riding == 35055
replace district_outcome = 1 if riding == 35056
replace district_outcome = 1 if riding == 35057
replace district_outcome = 2 if riding == 35058
replace district_outcome = 2 if riding == 35059
replace district_outcome = 2 if riding == 35060
replace district_outcome = 2 if riding == 35061
replace district_outcome = 2 if riding == 35062
replace district_outcome = 2 if riding == 35063
replace district_outcome = 2 if riding == 35064
replace district_outcome = 2 if riding == 35065
replace district_outcome = 2 if riding == 35066
replace district_outcome = 1 if riding == 35067
replace district_outcome = 1 if riding == 35068
replace district_outcome = 2 if riding == 35069
replace district_outcome = 2 if riding == 35070
replace district_outcome = 2 if riding == 35071
replace district_outcome = 2 if riding == 35072
replace district_outcome = 2 if riding == 35073
replace district_outcome = 1 if riding == 35074
replace district_outcome = 2 if riding == 35075
replace district_outcome = 2 if riding == 35076
replace district_outcome = 2 if riding == 35077
replace district_outcome = 2 if riding == 35078
replace district_outcome = 2 if riding == 35079
replace district_outcome = 1 if riding == 35080
replace district_outcome = 2 if riding == 35081
replace district_outcome = 1 if riding == 35082
replace district_outcome = 1 if riding == 35083
replace district_outcome = 2 if riding == 35084
replace district_outcome = 2 if riding == 35085
replace district_outcome = 1 if riding == 35086
replace district_outcome = 2 if riding == 35087
replace district_outcome = 1 if riding == 35088
replace district_outcome = 2 if riding == 35089
replace district_outcome = 2 if riding == 35090
replace district_outcome = 1 if riding == 35091
replace district_outcome = 2 if riding == 35092
replace district_outcome = 2 if riding == 35093
replace district_outcome = 2 if riding == 35094
replace district_outcome = 2 if riding == 35095
replace district_outcome = 2 if riding == 35096
replace district_outcome = 2 if riding == 35097
replace district_outcome = 2 if riding == 35098
replace district_outcome = 1 if riding == 35099
replace district_outcome = 1 if riding == 35100
replace district_outcome = 2 if riding == 35101
replace district_outcome = 1 if riding == 35102
replace district_outcome = 2 if riding == 35103
replace district_outcome = 1 if riding == 35104
replace district_outcome = 2 if riding == 35105
replace district_outcome = 2 if riding == 35106
replace district_outcome = 3 if riding == 35107
replace district_outcome = 2 if riding == 35108
replace district_outcome = 2 if riding == 35109
replace district_outcome = 2 if riding == 35110
replace district_outcome = 2 if riding == 35111
replace district_outcome = 2 if riding == 35112
replace district_outcome = 1 if riding == 35113
replace district_outcome = 2 if riding == 35114
replace district_outcome = 2 if riding == 35115
replace district_outcome = 3 if riding == 35116
replace district_outcome = 3 if riding == 35117
replace district_outcome = 2 if riding == 35118
replace district_outcome = 1 if riding == 35119
replace district_outcome = 2 if riding == 35120
replace district_outcome = 2 if riding == 35121
replace district_outcome = 1 if riding == 46001
replace district_outcome = 2 if riding == 46002
replace district_outcome = 3 if riding == 46003
replace district_outcome = 1 if riding == 46004
replace district_outcome = 3 if riding == 46005
replace district_outcome = 2 if riding == 46006
replace district_outcome = 1 if riding == 46007
replace district_outcome = 1 if riding == 46008
replace district_outcome = 2 if riding == 46009
replace district_outcome = 1 if riding == 46010
replace district_outcome = 2 if riding == 46011
replace district_outcome = 2 if riding == 46012
replace district_outcome = 2 if riding == 46013
replace district_outcome = 2 if riding == 46014
replace district_outcome = 1 if riding == 47001
replace district_outcome = 1 if riding == 47002
replace district_outcome = 3 if riding == 47003
replace district_outcome = 1 if riding == 47004
replace district_outcome = 1 if riding == 47005
replace district_outcome = 1 if riding == 47006
replace district_outcome = 3 if riding == 47007
replace district_outcome = 1 if riding == 47008
replace district_outcome = 2 if riding == 47009
replace district_outcome = 1 if riding == 47010
replace district_outcome = 1 if riding == 47011
replace district_outcome = 3 if riding == 47012
replace district_outcome = 1 if riding == 47013
replace district_outcome = 1 if riding == 47014
replace district_outcome = 1 if riding == 48001
replace district_outcome = 1 if riding == 48002
replace district_outcome = 1 if riding == 48003
replace district_outcome = 2 if riding == 48004
replace district_outcome = 1 if riding == 48005
replace district_outcome = 1 if riding == 48006
replace district_outcome = 1 if riding == 48007
replace district_outcome = 1 if riding == 48008
replace district_outcome = 1 if riding == 48009
replace district_outcome = 1 if riding == 48010
replace district_outcome = 1 if riding == 48011
replace district_outcome = 1 if riding == 48012
replace district_outcome = 2 if riding == 48013
replace district_outcome = 2 if riding == 48014
replace district_outcome = 1 if riding == 48015
replace district_outcome = 1 if riding == 48016
replace district_outcome = 2 if riding == 48017
replace district_outcome = 1 if riding == 48018
replace district_outcome = 3 if riding == 48019
replace district_outcome = 1 if riding == 48020
replace district_outcome = 1 if riding == 48021
replace district_outcome = 1 if riding == 48022
replace district_outcome = 1 if riding == 48023
replace district_outcome = 1 if riding == 48024
replace district_outcome = 1 if riding == 48025
replace district_outcome = 1 if riding == 48026
replace district_outcome = 1 if riding == 48027
replace district_outcome = 1 if riding == 48028
replace district_outcome = 1 if riding == 48029
replace district_outcome = 1 if riding == 48030
replace district_outcome = 1 if riding == 48031
replace district_outcome = 1 if riding == 48032
replace district_outcome = 1 if riding == 48033
replace district_outcome = 1 if riding == 48034
replace district_outcome = 1 if riding == 59001
replace district_outcome = 2 if riding == 59002
replace district_outcome = 3 if riding == 59003
replace district_outcome = 1 if riding == 59004
replace district_outcome = 1 if riding == 59005
replace district_outcome = 1 if riding == 59006
replace district_outcome = 2 if riding == 59007
replace district_outcome = 2 if riding == 59008
replace district_outcome = 3 if riding == 59009
replace district_outcome = 3 if riding == 59010
replace district_outcome = 2 if riding == 59011
replace district_outcome = 2 if riding == 59012
replace district_outcome = 1 if riding == 59013
replace district_outcome = 2 if riding == 59014
replace district_outcome = 3 if riding == 59015
replace district_outcome = 1 if riding == 59016
replace district_outcome = 2 if riding == 59017
replace district_outcome = 3 if riding == 59018
replace district_outcome = 3 if riding == 59019
replace district_outcome = 1 if riding == 59020
replace district_outcome = 2 if riding == 59021
replace district_outcome = 2 if riding == 59022
replace district_outcome = 3 if riding == 59023
replace district_outcome = 1 if riding == 59024
replace district_outcome = 1 if riding == 59025
replace district_outcome = 3 if riding == 59026
replace district_outcome = 5 if riding == 59027
replace district_outcome = 3 if riding == 59028
replace district_outcome = 3 if riding == 59029
replace district_outcome = 1 if riding == 59030
replace district_outcome = 2 if riding == 59031
replace district_outcome = 2 if riding == 59032
replace district_outcome = 2 if riding == 59033
replace district_outcome = 2 if riding == 59034
replace district_outcome = 3 if riding == 59035
replace district_outcome = 2 if riding == 59036
replace district_outcome = 3 if riding == 59037
replace district_outcome = 3 if riding == 59038
replace district_outcome = 2 if riding == 59039
replace district_outcome = 2 if riding == 59040
replace district_outcome = 3 if riding == 59041
replace district_outcome = 2 if riding == 59042
replace district_outcome = 2 if riding == 60001
replace district_outcome = 2 if riding == 61001
replace district_outcome = 2 if riding == 62001


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 38.1 if riding == 10001
replace margin = 71.7 if riding == 10002
replace margin = 56.5 if riding == 10003
replace margin = 57.4 if riding == 10004
replace margin = 61.7 if riding == 10005
replace margin = 1.4 if riding == 10006
replace margin = 21.1 if riding == 10007
replace margin = 48.9 if riding == 11001
replace margin = 33.1 if riding == 11002
replace margin = 20.3 if riding == 11003
replace margin = 44.5 if riding == 11004
replace margin = 59.9 if riding == 12001
replace margin = 32.7 if riding == 12002
replace margin = 37.3 if riding == 12003
replace margin = 33.8 if riding == 12004
replace margin = 15.6 if riding == 12005
replace margin = 53 if riding == 12006
replace margin = 52.2 if riding == 12007
replace margin = 13.6 if riding == 12008
replace margin = 34.4 if riding == 12009
replace margin = 60.1 if riding == 12010
replace margin = 36.9 if riding == 12011
replace margin = 11.3 if riding == 13001
replace margin = 53.9 if riding == 13002
replace margin = 20.8 if riding == 13003
replace margin = 3.8 if riding == 13004
replace margin = 29.8 if riding == 13005
replace margin = 13 if riding == 13006
replace margin = 36.3 if riding == 13007
replace margin = 5.4 if riding == 13008
replace margin = 18.3 if riding == 13009
replace margin = 9.6 if riding == 13010
replace margin = 4.9 if riding == 24001
replace margin = 11.9 if riding == 24002
replace margin = 16.8 if riding == 24003
replace margin = 20.5 if riding == 24004
replace margin = 18.5 if riding == 24005
replace margin = 18.5 if riding == 24006
replace margin = 36.6 if riding == 24007
replace margin = 5.1 if riding == 24008
replace margin = 15.7 if riding == 24009
replace margin = 30.2 if riding == 24010
replace margin = 1.7 if riding == 24011
replace margin = 16.4 if riding == 24012
replace margin = 5.4 if riding == 24013
replace margin = 0.4 if riding == 24014
replace margin = 36.9 if riding == 24015
replace margin = 19.4 if riding == 24016
replace margin = 25.7 if riding == 24017
replace margin = 15.1 if riding == 24018
replace margin = 19 if riding == 24019
replace margin = 6.6 if riding == 24020
replace margin = 14.7 if riding == 24021
replace margin = 1.4 if riding == 24022
replace margin = 9.5 if riding == 24023
replace margin = 33.3 if riding == 24024
replace margin = 3.9 if riding == 24025
replace margin = 6.2 if riding == 24026
replace margin = 27.2 if riding == 24027
replace margin = 1 if riding == 24028
replace margin = 40.1 if riding == 24029
replace margin = 19.9 if riding == 24030
replace margin = 5.1 if riding == 24031
replace margin = 0.7 if riding == 24032
replace margin = 5 if riding == 24033
replace margin = 10.2 if riding == 24034
replace margin = 4.8 if riding == 24035
replace margin = 46.7 if riding == 24036
replace margin = 14.9 if riding == 24037
replace margin = 2.4 if riding == 24038
replace margin = 9.6 if riding == 24039
replace margin = 27.9 if riding == 24040
replace margin = 8.4 if riding == 24041
replace margin = 28.4 if riding == 24042
replace margin = 1.2 if riding == 24043
replace margin = 7.7 if riding == 24044
replace margin = 29 if riding == 24045
replace margin = 11.9 if riding == 24046
replace margin = 7.3 if riding == 24047
replace margin = 1.4 if riding == 24048
replace margin = 4.1 if riding == 24049
replace margin = 9.3 if riding == 24050
replace margin = 0.6 if riding == 24051
replace margin = 12.5 if riding == 24052
replace margin = 35.9 if riding == 24053
replace margin = 10.6 if riding == 24054
replace margin = 26.1 if riding == 24055
replace margin = 38.7 if riding == 24056
replace margin = 32 if riding == 24057
replace margin = 21.9 if riding == 24058
replace margin = 1.9 if riding == 24059
replace margin = 7.4 if riding == 24060
replace margin = 6.9 if riding == 24061
replace margin = 2.9 if riding == 24062
replace margin = 1.9 if riding == 24063
replace margin = 28.1 if riding == 24064
replace margin = 17.4 if riding == 24065
replace margin = 1.1 if riding == 24066
replace margin = 4.1 if riding == 24067
replace margin = 42.1 if riding == 24068
replace margin = 49.9 if riding == 24069
replace margin = 20.7 if riding == 24070
replace margin = 1.3 if riding == 24071
replace margin = 15.3 if riding == 24072
replace margin = 7.6 if riding == 24073
replace margin = 24.3 if riding == 24074
replace margin = 5 if riding == 24075
replace margin = 1.6 if riding == 24076
replace margin = 27.4 if riding == 24077
replace margin = 25.2 if riding == 24078
replace margin = 21.5 if riding == 35001
replace margin = 5.8 if riding == 35002
replace margin = 2.1 if riding == 35003
replace margin = 9.3 if riding == 35004
replace margin = 0.2 if riding == 35005
replace margin = 16.5 if riding == 35006
replace margin = 18.6 if riding == 35007
replace margin = 15 if riding == 35008
replace margin = 28.8 if riding == 35009
replace margin = 15.4 if riding == 35010
replace margin = 17.1 if riding == 35011
replace margin = 25.8 if riding == 35012
replace margin = 10.2 if riding == 35013
replace margin = 7.8 if riding == 35014
replace margin = 3.5 if riding == 35015
replace margin = 4.5 if riding == 35016
replace margin = 4.5 if riding == 35017
replace margin = 2.9 if riding == 35018
replace margin = 28.6 if riding == 35019
replace margin = 13.6 if riding == 35020
replace margin = 16.2 if riding == 35021
replace margin = 7.2 if riding == 35022
replace margin = 9.4 if riding == 35023
replace margin = 6.3 if riding == 35024
replace margin = 18.2 if riding == 35025
replace margin = 5.7 if riding == 35026
replace margin = 15.4 if riding == 35027
replace margin = 21.2 if riding == 35028
replace margin = 39.4 if riding == 35029
replace margin = 4.3 if riding == 35030
replace margin = 16.9 if riding == 35031
replace margin = 22.8 if riding == 35032
replace margin = 7.5 if riding == 35033
replace margin = 13.1 if riding == 35034
replace margin = 12.2 if riding == 35035
replace margin = 6.3 if riding == 35036
replace margin = 2.4 if riding == 35037
replace margin = 15.9 if riding == 35038
replace margin = 0.5 if riding == 35039
replace margin = 5.2 if riding == 35040
replace margin = 12.1 if riding == 35041
replace margin = 1.6 if riding == 35042
replace margin = 3.2 if riding == 35043
replace margin = 32.7 if riding == 35044
replace margin = 18.4 if riding == 35045
replace margin = 0.5 if riding == 35046
replace margin = 5.6 if riding == 35047
replace margin = 20.8 if riding == 35048
replace margin = 14.1 if riding == 35049
replace margin = 6.8 if riding == 35050
replace margin = 6.3 if riding == 35051
replace margin = 19.4 if riding == 35052
replace margin = 10.5 if riding == 35053
replace margin = 6.4 if riding == 35054
replace margin = 23.4 if riding == 35055
replace margin = 6 if riding == 35056
replace margin = 4.9 if riding == 35057
replace margin = 21.1 if riding == 35058
replace margin = 18.9 if riding == 35059
replace margin = 10.5 if riding == 35060
replace margin = 6.5 if riding == 35061
replace margin = 32.7 if riding == 35062
replace margin = 7.4 if riding == 35063
replace margin = 16.3 if riding == 35064
replace margin = 2.6 if riding == 35065
replace margin = 4.2 if riding == 35066
replace margin = 7.6 if riding == 35067
replace margin = 16.1 if riding == 35068
replace margin = 5 if riding == 35069
replace margin = 22.6 if riding == 35070
replace margin = 3 if riding == 35071
replace margin = 6.9 if riding == 35072
replace margin = 3.4 if riding == 35073
replace margin = 6.3 if riding == 35074
replace margin = 4.1 if riding == 35075
replace margin = 29.1 if riding == 35076
replace margin = 35.8 if riding == 35077
replace margin = 38.3 if riding == 35078
replace margin = 25.9 if riding == 35079
replace margin = 13.5 if riding == 35080
replace margin = 1.8 if riding == 35081
replace margin = 4.4 if riding == 35082
replace margin = 5.4 if riding == 35083
replace margin = 8.8 if riding == 35084
replace margin = 12.1 if riding == 35085
replace margin = 13.2 if riding == 35086
replace margin = 3.6 if riding == 35087
replace margin = 3.1 if riding == 35088
replace margin = 5.6 if riding == 35089
replace margin = 28.3 if riding == 35090
replace margin = 7.7 if riding == 35091
replace margin = 13.6 if riding == 35092
replace margin = 13.9 if riding == 35093
replace margin = 17.9 if riding == 35094
replace margin = 33.5 if riding == 35095
replace margin = 20.8 if riding == 35096
replace margin = 32.9 if riding == 35097
replace margin = 28.7 if riding == 35098
replace margin = 8 if riding == 35099
replace margin = 3.7 if riding == 35100
replace margin = 27.4 if riding == 35101
replace margin = 12.5 if riding == 35102
replace margin = 19.6 if riding == 35103
replace margin = 24.8 if riding == 35104
replace margin = 14.4 if riding == 35105
replace margin = 21.8 if riding == 35106
replace margin = 8.1 if riding == 35107
replace margin = 31.3 if riding == 35108
replace margin = 2.2 if riding == 35109
replace margin = 21.2 if riding == 35110
replace margin = 4.9 if riding == 35111
replace margin = 17.4 if riding == 35112
replace margin = 14.4 if riding == 35113
replace margin = 2.9 if riding == 35114
replace margin = 16.4 if riding == 35115
replace margin = 16 if riding == 35116
replace margin = 26.1 if riding == 35117
replace margin = 2.9 if riding == 35118
replace margin = 12.5 if riding == 35119
replace margin = 15.6 if riding == 35120
replace margin = 46.8 if riding == 35121
replace margin = 13 if riding == 46001
replace margin = 13 if riding == 46002
replace margin = 3 if riding == 46003
replace margin = 16.8 if riding == 46004
replace margin = 0.1 if riding == 46005
replace margin = 2.8 if riding == 46006
replace margin = 35.1 if riding == 46007
replace margin = 21.4 if riding == 46008
replace margin = 29.8 if riding == 46009
replace margin = 20.5 if riding == 46010
replace margin = 26.5 if riding == 46011
replace margin = 53.6 if riding == 46012
replace margin = 23.6 if riding == 46013
replace margin = 31.5 if riding == 46014
replace margin = 43.4 if riding == 47001
replace margin = 54.3 if riding == 47002
replace margin = 0.3 if riding == 47003
replace margin = 46.1 if riding == 47004
replace margin = 31.7 if riding == 47005
replace margin = 21.3 if riding == 47006
replace margin = 0.3 if riding == 47007
replace margin = 14.5 if riding == 47008
replace margin = 24.9 if riding == 47009
replace margin = 11.4 if riding == 47010
replace margin = 10 if riding == 47011
replace margin = 6.7 if riding == 47012
replace margin = 56.5 if riding == 47013
replace margin = 39 if riding == 47014
replace margin = 37.3 if riding == 48001
replace margin = 71.5 if riding == 48002
replace margin = 63.7 if riding == 48003
replace margin = 1.2 if riding == 48004
replace margin = 2.4 if riding == 48005
replace margin = 12 if riding == 48006
replace margin = 37.8 if riding == 48007
replace margin = 44.1 if riding == 48008
replace margin = 33.2 if riding == 48009
replace margin = 28.7 if riding == 48010
replace margin = 41.2 if riding == 48011
replace margin = 30 if riding == 48012
replace margin = 6.1 if riding == 48013
replace margin = 2.2 if riding == 48014
replace margin = 5.9 if riding == 48015
replace margin = 17.7 if riding == 48016
replace margin = 0.2 if riding == 48017
replace margin = 19.7 if riding == 48018
replace margin = 12.7 if riding == 48019
replace margin = 14.4 if riding == 48020
replace margin = 44.3 if riding == 48021
replace margin = 62.3 if riding == 48022
replace margin = 32.2 if riding == 48023
replace margin = 58.3 if riding == 48024
replace margin = 59.1 if riding == 48025
replace margin = 36.3 if riding == 48026
replace margin = 50.9 if riding == 48027
replace margin = 55 if riding == 48028
replace margin = 60.9 if riding == 48029
replace margin = 55.7 if riding == 48030
replace margin = 22.7 if riding == 48031
replace margin = 43.5 if riding == 48032
replace margin = 54.7 if riding == 48033
replace margin = 58 if riding == 48034
replace margin = 15.5 if riding == 59001
replace margin = 6.5 if riding == 59002
replace margin = 1.2 if riding == 59003
replace margin = 5.1 if riding == 59004
replace margin = 2.4 if riding == 59005
replace margin = 8.5 if riding == 59006
replace margin = 10.8 if riding == 59007
replace margin = 3.3 if riding == 59008
replace margin = 9.8 if riding == 59009
replace margin = 12.2 if riding == 59010
replace margin = 16.3 if riding == 59011
replace margin = 17.6 if riding == 59012
replace margin = 4.5 if riding == 59013
replace margin = 6.4 if riding == 59014
replace margin = 0.4 if riding == 59015
replace margin = 9.1 if riding == 59016
replace margin = 2.3 if riding == 59017
replace margin = 9.7 if riding == 59018
replace margin = 14.5 if riding == 59019
replace margin = 9.4 if riding == 59020
replace margin = 29.8 if riding == 59021
replace margin = 2.5 if riding == 59022
replace margin = 5.2 if riding == 59023
replace margin = 27.6 if riding == 59024
replace margin = 2.8 if riding == 59025
replace margin = 7.7 if riding == 59026
replace margin = 34.9 if riding == 59027
replace margin = 26.3 if riding == 59028
replace margin = 7.4 if riding == 59029
replace margin = 2.5 if riding == 59030
replace margin = 6.6 if riding == 59031
replace margin = 15 if riding == 59032
replace margin = 29.9 if riding == 59033
replace margin = 36.1 if riding == 59034
replace margin = 21.8 if riding == 59035
replace margin = 17.1 if riding == 59036
replace margin = 14 if riding == 59037
replace margin = 17.9 if riding == 59038
replace margin = 32.9 if riding == 59039
replace margin = 14.9 if riding == 59040
replace margin = 9.4 if riding == 59041
replace margin = 28.4 if riding == 59042
replace margin = 29.4 if riding == 60001
replace margin = 17.9 if riding == 61001
replace margin = 20.5 if riding == 62001

* Effective number of electoral parties by district

gen enep = .

replace enep = 2.650755068 if riding == 10001
replace enep = 1.460455253 if riding == 10002
replace enep = 1.675664988 if riding == 10003
replace enep = 1.799917564 if riding == 10004
replace enep = 1.740634949 if riding == 10005
replace enep = 2.338344405 if riding == 10006
replace enep = 2.114857934 if riding == 10007
replace enep = 2.149012529 if riding == 11001
replace enep = 2.527818644 if riding == 11002
replace enep = 2.742062415 if riding == 11003
replace enep = 2.285061411 if riding == 11004
replace enep = 1.718520148 if riding == 12001
replace enep = 2.375054329 if riding == 12002
replace enep = 2.08090994 if riding == 12003
replace enep = 2.386543712 if riding == 12004
replace enep = 2.462453737 if riding == 12005
replace enep = 1.959335942 if riding == 12006
replace enep = 1.852791972 if riding == 12007
replace enep = 2.690189685 if riding == 12008
replace enep = 2.475639705 if riding == 12009
replace enep = 1.770281227 if riding == 12010
replace enep = 2.121300187 if riding == 12011
replace enep = 2.388972503 if riding == 13001
replace enep = 1.945820572 if riding == 13002
replace enep = 2.866298634 if riding == 13003
replace enep = 2.96644359 if riding == 13004
replace enep = 2.469648026 if riding == 13005
replace enep = 2.732285229 if riding == 13006
replace enep = 2.446968084 if riding == 13007
replace enep = 2.777824075 if riding == 13008
replace enep = 2.756681507 if riding == 13009
replace enep = 2.706902059 if riding == 13010
replace enep = 3.528668669 if riding == 24001
replace enep = 3.304605298 if riding == 24002
replace enep = 3.009845204 if riding == 24003
replace enep = 3.327853468 if riding == 24004
replace enep = 3.369646895 if riding == 24005
replace enep = 3.872501752 if riding == 24006
replace enep = 2.428221765 if riding == 24007
replace enep = 4.06944094 if riding == 24008
replace enep = 3.553470853 if riding == 24009
replace enep = 2.987973407 if riding == 24010
replace enep = 3.724491886 if riding == 24011
replace enep = 3.372487918 if riding == 24012
replace enep = 3.890218047 if riding == 24013
replace enep = 4.191764859 if riding == 24014
replace enep = 2.82843825 if riding == 24015
replace enep = 3.365484933 if riding == 24016
replace enep = 2.932568519 if riding == 24017
replace enep = 3.254191399 if riding == 24018
replace enep = 3.47316287 if riding == 24019
replace enep = 3.918065416 if riding == 24020
replace enep = 3.588190547 if riding == 24021
replace enep = 3.922306944 if riding == 24022
replace enep = 3.702222815 if riding == 24023
replace enep = 2.698858653 if riding == 24024
replace enep = 4.046305925 if riding == 24025
replace enep = 3.300765778 if riding == 24026
replace enep = 2.659369357 if riding == 24027
replace enep = 3.740541107 if riding == 24028
replace enep = 2.647800208 if riding == 24029
replace enep = 2.673982683 if riding == 24030
replace enep = 3.741744776 if riding == 24031
replace enep = 4.006426308 if riding == 24032
replace enep = 3.65817969 if riding == 24033
replace enep = 3.718467396 if riding == 24034
replace enep = 1.490464011 if riding == 24035
replace enep = 2.178122502 if riding == 24036
replace enep = 3.210211039 if riding == 24037
replace enep = 3.697910311 if riding == 24038
replace enep = 3.468958029 if riding == 24039
replace enep = 3.172910955 if riding == 24040
replace enep = 3.753767844 if riding == 24041
replace enep = 3.000138006 if riding == 24042
replace enep = 3.702661473 if riding == 24043
replace enep = 3.850344798 if riding == 24044
replace enep = 2.96672521 if riding == 24045
replace enep = 3.350139868 if riding == 24046
replace enep = 3.727907488 if riding == 24047
replace enep = 3.722564698 if riding == 24048
replace enep = 3.848107501 if riding == 24049
replace enep = 3.657724749 if riding == 24050
replace enep = 4.006956076 if riding == 24051
replace enep = 2.476246604 if riding == 24052
replace enep = 2.482621648 if riding == 24053
replace enep = 3.085238983 if riding == 24054
replace enep = 2.812812925 if riding == 24055
replace enep = 2.421805941 if riding == 24056
replace enep = 2.687962498 if riding == 24057
replace enep = 3.328462683 if riding == 24058
replace enep = 4.164185506 if riding == 24059
replace enep = 3.82187027 if riding == 24060
replace enep = 4.010411027 if riding == 24061
replace enep = 3.732025632 if riding == 24062
replace enep = 3.695873926 if riding == 24063
replace enep = 3.008849025 if riding == 24064
replace enep = 3.519676753 if riding == 24065
replace enep = 4.064429334 if riding == 24066
replace enep = 3.7242006 if riding == 24067
replace enep = 2.306805075 if riding == 24068
replace enep = 2.181734086 if riding == 24069
replace enep = 3.580482076 if riding == 24070
replace enep = 3.7236043 if riding == 24071
replace enep = 3.64281473 if riding == 24072
replace enep = 3.576768175 if riding == 24073
replace enep = 3.237157387 if riding == 24074
replace enep = 3.760769905 if riding == 24075
replace enep = 3.903977763 if riding == 24076
replace enep = 2.970055896 if riding == 24077
replace enep = 3.289581894 if riding == 24078
replace enep = 2.284471533 if riding == 35001
replace enep = 3.010823912 if riding == 35002
replace enep = 2.317626009 if riding == 35003
replace enep = 2.713807309 if riding == 35004
replace enep = 2.775172199 if riding == 35005
replace enep = 2.565029921 if riding == 35006
replace enep = 2.728498071 if riding == 35007
replace enep = 2.680799843 if riding == 35008
replace enep = 2.619343854 if riding == 35009
replace enep = 2.697264434 if riding == 35010
replace enep = 2.463818821 if riding == 35011
replace enep = 2.388265021 if riding == 35012
replace enep = 3.088974831 if riding == 35013
replace enep = 2.617506406 if riding == 35014
replace enep = 2.493255743 if riding == 35015
replace enep = 2.808594299 if riding == 35016
replace enep = 2.883024177 if riding == 35017
replace enep = 2.632472603 if riding == 35018
replace enep = 2.321047257 if riding == 35019
replace enep = 2.410864319 if riding == 35020
replace enep = 2.300172513 if riding == 35021
replace enep = 2.646146681 if riding == 35022
replace enep = 2.794654385 if riding == 35023
replace enep = 2.354037881 if riding == 35024
replace enep = 2.75534468 if riding == 35025
replace enep = 2.91640419 if riding == 35026
replace enep = 2.356345403 if riding == 35027
replace enep = 2.464389571 if riding == 35028
replace enep = 2.184130978 if riding == 35029
replace enep = 2.755868622 if riding == 35030
replace enep = 2.363027132 if riding == 35031
replace enep = 2.963129776 if riding == 35032
replace enep = 2.870989228 if riding == 35033
replace enep = 2.932001032 if riding == 35034
replace enep = 2.917067763 if riding == 35035
replace enep = 3.089051167 if riding == 35036
replace enep = 3.245846939 if riding == 35037
replace enep = 2.8012303 if riding == 35038
replace enep = 2.685955408 if riding == 35039
replace enep = 2.654730996 if riding == 35040
replace enep = 2.368624256 if riding == 35041
replace enep = 3.101236773 if riding == 35042
replace enep = 2.355013588 if riding == 35043
replace enep = 2.56808639 if riding == 35044
replace enep = 2.784111632 if riding == 35045
replace enep = 2.622902661 if riding == 35046
replace enep = 2.944788167 if riding == 35047
replace enep = 2.714227165 if riding == 35048
replace enep = 2.74092753 if riding == 35049
replace enep = 2.512960594 if riding == 35050
replace enep = 3.161145726 if riding == 35051
replace enep = 2.669122259 if riding == 35052
replace enep = 2.799685315 if riding == 35053
replace enep = 2.329210631 if riding == 35054
replace enep = 2.346475945 if riding == 35055
replace enep = 2.300913463 if riding == 35056
replace enep = 2.618884251 if riding == 35057
replace enep = 2.371837747 if riding == 35058
replace enep = 2.343511987 if riding == 35059
replace enep = 2.440423169 if riding == 35060
replace enep = 2.473545432 if riding == 35061
replace enep = 2.301919801 if riding == 35062
replace enep = 2.497789456 if riding == 35063
replace enep = 2.426071535 if riding == 35064
replace enep = 2.53968254 if riding == 35065
replace enep = 3.169692667 if riding == 35066
replace enep = 2.936236684 if riding == 35067
replace enep = 2.777546316 if riding == 35068
replace enep = 2.820190306 if riding == 35069
replace enep = 2.616910475 if riding == 35070
replace enep = 2.775418672 if riding == 35071
replace enep = 2.33303626 if riding == 35072
replace enep = 2.432267433 if riding == 35073
replace enep = 3.097509602 if riding == 35074
replace enep = 2.836557554 if riding == 35075
replace enep = 2.19222287 if riding == 35076
replace enep = 2.300950523 if riding == 35077
replace enep = 2.462144528 if riding == 35078
replace enep = 2.421207844 if riding == 35079
replace enep = 2.928909508 if riding == 35080
replace enep = 2.809612245 if riding == 35081
replace enep = 2.82323181 if riding == 35082
replace enep = 2.86669304 if riding == 35083
replace enep = 2.853050767 if riding == 35084
replace enep = 2.451425013 if riding == 35085
replace enep = 2.971326697 if riding == 35086
replace enep = 2.414648222 if riding == 35087
replace enep = 2.405349497 if riding == 35088
replace enep = 2.809762238 if riding == 35089
replace enep = 2.49241061 if riding == 35090
replace enep = 3.10002542 if riding == 35091
replace enep = 2.895244272 if riding == 35092
replace enep = 2.379468046 if riding == 35093
replace enep = 2.653843296 if riding == 35094
replace enep = 2.256241328 if riding == 35095
replace enep = 2.805064825 if riding == 35096
replace enep = 2.228680443 if riding == 35097
replace enep = 2.649610242 if riding == 35098
replace enep = 2.649933222 if riding == 35099
replace enep = 2.769722502 if riding == 35100
replace enep = 2.507315092 if riding == 35101
replace enep = 2.400649136 if riding == 35102
replace enep = 2.878542767 if riding == 35103
replace enep = 2.170996357 if riding == 35104
replace enep = 3.039199596 if riding == 35105
replace enep = 3.271394923 if riding == 35106
replace enep = 2.8862861 if riding == 35107
replace enep = 2.37203466 if riding == 35108
replace enep = 2.831994744 if riding == 35109
replace enep = 2.767821309 if riding == 35110
replace enep = 2.312673451 if riding == 35111
replace enep = 2.671054319 if riding == 35112
replace enep = 2.49498508 if riding == 35113
replace enep = 2.558526289 if riding == 35114
replace enep = 2.339372814 if riding == 35115
replace enep = 2.975915913 if riding == 35116
replace enep = 2.699237465 if riding == 35117
replace enep = 2.385416518 if riding == 35118
replace enep = 2.476657503 if riding == 35119
replace enep = 2.925208275 if riding == 35120
replace enep = 1.99957609 if riding == 35121
replace enep = 2.501075462 if riding == 46001
replace enep = 2.34224401 if riding == 46002
replace enep = 2.564668106 if riding == 46003
replace enep = 3.081398215 if riding == 46004
replace enep = 3.130066796 if riding == 46005
replace enep = 2.765211428 if riding == 46006
replace enep = 2.258886459 if riding == 46007
replace enep = 2.275110969 if riding == 46008
replace enep = 2.297846917 if riding == 46009
replace enep = 2.614365415 if riding == 46010
replace enep = 2.547465654 if riding == 46011
replace enep = 1.935501353 if riding == 46012
replace enep = 2.158712889 if riding == 46013
replace enep = 2.247221311 if riding == 46014
replace enep = 2.316884994 if riding == 47001
replace enep = 1.925973291 if riding == 47002
replace enep = 3.097797466 if riding == 47003
replace enep = 2.10615883 if riding == 47004
replace enep = 2.514945061 if riding == 47005
replace enep = 2.71154086 if riding == 47006
replace enep = 3.108582797 if riding == 47007
replace enep = 2.910987815 if riding == 47008
replace enep = 2.428793837 if riding == 47009
replace enep = 2.991504128 if riding == 47010
replace enep = 2.983444864 if riding == 47011
replace enep = 3.072611966 if riding == 47012
replace enep = 1.890105487 if riding == 47013
replace enep = 2.359959975 if riding == 47014
replace enep = 2.100200569 if riding == 48001
replace enep = 1.495738641 if riding == 48002
replace enep = 1.610217798 if riding == 48003
replace enep = 2.352553697 if riding == 48004
replace enep = 2.463205862 if riding == 48005
replace enep = 2.695323344 if riding == 48006
replace enep = 2.08144269 if riding == 48007
replace enep = 1.98954692 if riding == 48008
replace enep = 2.26713672 if riding == 48009
replace enep = 2.131696191 if riding == 48010
replace enep = 1.997642782 if riding == 48011
replace enep = 2.15458269 if riding == 48012
replace enep = 2.655365431 if riding == 48013
replace enep = 3.109269042 if riding == 48014
replace enep = 3.092193757 if riding == 48015
replace enep = 2.9692093 if riding == 48016
replace enep = 2.810899544 if riding == 48017
replace enep = 2.702856108 if riding == 48018
replace enep = 2.984940973 if riding == 48019
replace enep = 2.616766671 if riding == 48020
replace enep = 2.044254011 if riding == 48021
replace enep = 1.677078529 if riding == 48022
replace enep = 2.201397007 if riding == 48023
replace enep = 1.783507196 if riding == 48024
replace enep = 1.786977934 if riding == 48025
replace enep = 2.501657348 if riding == 48026
replace enep = 1.93960083 if riding == 48027
replace enep = 1.925068629 if riding == 48028
replace enep = 1.730669717 if riding == 48029
replace enep = 1.86502445 if riding == 48030
replace enep = 3.262876944 if riding == 48031
replace enep = 2.170162437 if riding == 48032
replace enep = 1.893362062 if riding == 48033
replace enep = 1.811298882 if riding == 48034
replace enep = 2.761317259 if riding == 59001
replace enep = 3.354849099 if riding == 59002
replace enep = 3.200327714 if riding == 59003
replace enep = 3.320064144 if riding == 59004
replace enep = 2.994280923 if riding == 59005
replace enep = 3.043454443 if riding == 59006
replace enep = 2.821200759 if riding == 59007
replace enep = 3.297815857 if riding == 59008
replace enep = 3.49770201 if riding == 59009
replace enep = 3.757900987 if riding == 59010
replace enep = 2.688967168 if riding == 59011
replace enep = 2.835986399 if riding == 59012
replace enep = 3.193000942 if riding == 59013
replace enep = 2.552785216 if riding == 59014
replace enep = 3.163976232 if riding == 59015
replace enep = 2.781378117 if riding == 59016
replace enep = 3.27641115 if riding == 59017
replace enep = 3.854856927 if riding == 59018
replace enep = 3.162365323 if riding == 59019
replace enep = 3.204203916 if riding == 59020
replace enep = 2.458041236 if riding == 59021
replace enep = 3.300569678 if riding == 59022
replace enep = 3.19217534 if riding == 59023
replace enep = 2.74305527 if riding == 59024
replace enep = 2.625871133 if riding == 59025
replace enep = 3.734046287 if riding == 59026
replace enep = 2.701629353 if riding == 59027
replace enep = 2.783855863 if riding == 59028
replace enep = 3.239453958 if riding == 59029
replace enep = 2.646720977 if riding == 59030
replace enep = 2.719815488 if riding == 59031
replace enep = 2.98856277 if riding == 59032
replace enep = 2.457884155 if riding == 59033
replace enep = 2.58552931 if riding == 59034
replace enep = 2.86717798 if riding == 59035
replace enep = 2.992542584 if riding == 59036
replace enep = 3.311291172 if riding == 59037
replace enep = 3.016354675 if riding == 59038
replace enep = 2.354137632 if riding == 59039
replace enep = 2.678286861 if riding == 59040
replace enep = 3.173797131 if riding == 59041
replace enep = 2.600814055 if riding == 59042
replace enep = 2.592486973 if riding == 60001
replace enep = 2.77325583 if riding == 61001
replace enep = 2.822259727 if riding == 62001


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen time = 0

* Gender

gen male = gender

recode male (2=0)

* Age

gen r_age = ageval

replace r_age = . if r_age < 17

* Education

gen education = caedu2

* Vote status (national)

gen vote_whole = 1 if decided == 2
replace vote_whole = 0 if decided!=2 & decided!=. 

* Vote status (district)

gen vote_district = 1 if decided == district_outcome
replace vote_district = 0 if decided!=district_outcome & decided!=. 

* Political discussion

gen discussion = q32

recode discussion (5=.)

replace discussion = 4 - discussion

gen discussion_3pts = 0 if discussion == 0
replace discussion_3pts = 0 if discussion == 1
replace discussion_3pts = 1 if discussion == 2
replace discussion_3pts = 2 if discussion == 3


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen forecast_whole = q10b

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=2 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 2


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen forecasts_district = q10a

* Identify correct forecasts

gen correct_district_d = 0 if forecasts_district!=district_outcome & forecasts_district!=.
replace correct_district_d = 1 if forecasts_district == district_outcome


**********
** SAVE **
**********

save "ca2015_ipsos.dta", replace


******************************
** MAIN ANALYSES (NATIONAL) **
******************************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion i.vote_whole r_age i.male education
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


***********************************
** SUMMARY STATISTICS (NATIONAL) **
***********************************

label variable correct_whole_d "Election winner (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable vote_whole "National vote"
label variable vote_district "District vote"
label variable r_age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable enep "ENEP"
label variable margin "Margin"

estpost tabstat correct_whole_d discussion vote_whole r_age male education if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "ca2015_ipsos_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2015 Canadian federal election (Ipsos), national level") replace

eststo clear


******************************
** MAIN ANALYSES (DISTRICT) **
******************************

* Regression analysis and percentage of correct and incorrect forecasts (district)

melogit correct_district_d discussion i.vote_district r_age i.male education enep || riding: , or
estimates table, star(.05 .01 .001)

tab correct_district_d if e(sample) == 1


***********************************
** SUMMARY STATISTICS (DISTRICT) **
***********************************

label variable correct_whole_d "Election winner (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable vote_whole "National vote"
label variable vote_district "District vote"
label variable r_age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable enep "ENEP"
label variable margin "Margin"

estpost tabstat correct_district_d discussion vote_district r_age male education if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "ca2015_ipsos_summary_district.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2015 Canadian federal election (Ipsos), district level") replace

eststo clear

log close